#!/usr/bin/env python 3.6
# -*- coding: utf-8 -*-
# @Time      :2018/10/31
# @Author    :Tian Maohong
# @Email     :tianmaohong@bbdservice.com
# @File      :Ent_Loan_transfer.py
# @Software  :PyCharm

'''eof
name: 申请企业“转出”类信贷业务校验
code: Ent_Loan_transfer
tableName:
columnName:
groups:企业征信校验
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

"""
1.select count * as factor1 from 已结清正常类贷款 where 还款方式 包含“转出”；
2.select count * as factor2 from 已结清不良、关注类贷款 where 还款方式 包含“转出”；
3.select count * as factor3 from 已结清类贷款 where 结清方式 包含“转出”；
4.select count * as factor4 from 已结清贸易融资 where 还款方式 包含“转出”；
5.Factor = factor1 + factor2 + factor3
"""

import json
import sys


# reload(sys)
# sys.setdefaultencoding('utf-8')

# with open('data.json', 'r') as ECR_load:
#     EC = json.load(ECR_load)['EC']



def FormatData(tableName):
    """
    数据解析
    :param tableName: 目标字段
    :return:
    """
    try:
        # ecr是最外面一层表里面的
        ecr = json.loads(EC[0]["ecr"])[tableName]
        return ecr if isinstance(ecr, list) and len(ecr) > 0 else [{}]
    except:
        return [{}]


def ConvertDataType(dataValue, dataType):
    """
    数据格式转换
    :param dataValue:
    :param dataType: 数据类型
    :return:
    """
    returnData = None
    if dataType == 'float':
        try:
            returnData = float(dataValue) if float(dataValue) >= 0 else None
        except:
            returnData = None
    elif dataType == 'None':
        returnData = 0 if dataValue is None else dataValue
    elif dataType == 'str':
        try:
            returnData = dataValue if len(dataValue) >= 0 else None
        except:
            returnData = None

    return returnData

def Ent_Loan_transfer():
    """
    申请企业有“转出”类信贷业务
    :return:
    """
    try:
        # 已结清正常类贷款 信息汇总
        ECRPaidNormalLoanInfo = FormatData("ECRPaidNormalLoanInfo")
        # 已结清不良、关注类贷款 信息汇总
        ECRUnpaidSMLoanInfo = FormatData("ECRPaidBadLoanInfo")
        # 已结清贸易融资 信息汇总
        ECRTradeFinancingInfo = FormatData("ECRTradeFinancing")

        if ECRPaidNormalLoanInfo == [{}] and ECRUnpaidSMLoanInfo == [{}] and ECRTradeFinancingInfo == [{}]:
            return u'缺失值'

        else:
            # 已结清正常类贷款
            ECRPaidNormalLoanCount = []
            for i in range(len(ECRPaidNormalLoanInfo)):
                # 已结清正常类贷款
                ECRPaidNormalLoanInfoPaidtype = ConvertDataType(ECRPaidNormalLoanInfo[i].get("Paidtype"), 'str')
                ECRPaidNormalLoanInfoLoanPaidForm = ConvertDataType(ECRPaidNormalLoanInfo[i].get("LoanPaidForm"), 'str')
                ECRPaidNormalLoanInfoFiveclasscode = ConvertDataType(ECRPaidNormalLoanInfo[i].get("Fiveclasscode"), 'str')

                if ECRPaidNormalLoanInfoPaidtype is None and ECRPaidNormalLoanInfoLoanPaidForm is None and ECRPaidNormalLoanInfoFiveclasscode is None:
                    return u'缺失值'
                else:
                    if ECRPaidNormalLoanInfoPaidtype == u'已结清' and u'转出'in ECRPaidNormalLoanInfoLoanPaidForm and \
                                    ECRPaidNormalLoanInfoFiveclasscode == u'正常':
                        ECRPaidNormalLoanInfoFormCount = 1
                    else:
                        ECRPaidNormalLoanInfoFormCount = 0

                    ECRPaidNormalLoanCount.append(ECRPaidNormalLoanInfoFormCount)

            # 已结清不良、关注类贷款
            ECRUnpaidSMLoanCoun = []
            for i in range(len(ECRUnpaidSMLoanInfo)):
                ECRUnpaidSMLoanInfoPaidType = ConvertDataType(ECRUnpaidSMLoanInfo[i].get("PaidType"), 'str')
                ECRUnpaidSMLoanInfoRepaymethod = ConvertDataType(ECRUnpaidSMLoanInfo[i].get("Repaymethod"), 'str')
                ECRUnpaidSMLoanInfoFiveclasscode = ConvertDataType(ECRUnpaidSMLoanInfo[i].get("Fiveclasscode"), 'str')

                if ECRUnpaidSMLoanInfoPaidType is None and ECRUnpaidSMLoanInfoRepaymethod is None and ECRUnpaidSMLoanInfoFiveclasscode is None:
                    return u'缺失值'
                else:
                    if ECRUnpaidSMLoanInfoPaidType == u'已结清' and u'转出' in ECRUnpaidSMLoanInfoRepaymethod and (ECRUnpaidSMLoanInfoFiveclasscode == u'次级' or ECRUnpaidSMLoanInfoFiveclasscode == u'关注'\
                            or ECRUnpaidSMLoanInfoFiveclasscode == u'可疑' or ECRUnpaidSMLoanInfoFiveclasscode == u'损失'):
                        ECRUnpaidSMLoanInfoFormCount = 1
                    else:
                        ECRUnpaidSMLoanInfoFormCount = 0

                    ECRUnpaidSMLoanCoun.append(ECRUnpaidSMLoanInfoFormCount)

            # 已结清贸易融资
            ECRUnpaidBadLoanCount = []
            for i in range(len(ECRTradeFinancingInfo)):
                # 已结清贸易融资
                ECRTradeFinancingInfoPaidType = ConvertDataType(ECRTradeFinancingInfo[i].get("PaidType"), 'str')
                ECRTradeFinancingInfoRepaymethod = ConvertDataType(ECRTradeFinancingInfo[i].get("Repaymethod"), 'str')

                if ECRTradeFinancingInfoPaidType is None and ECRTradeFinancingInfoRepaymethod is None:
                    return u'缺失值'
                else:
                    if ECRTradeFinancingInfoPaidType == u'已结清' and u'转出' in ECRTradeFinancingInfoRepaymethod:
                        ECRUnpaidBadLoanInfoFormCount = 1
                    else:
                        ECRUnpaidBadLoanInfoFormCount = 0

                    ECRUnpaidBadLoanCount.append(ECRUnpaidBadLoanInfoFormCount)

            # 账户汇总
            FormCount = ECRPaidNormalLoanCount + ECRUnpaidSMLoanCoun + ECRUnpaidBadLoanCount

        return sum(FormCount)
    except:
        # 异常提示或者异常出口
        return None

result = Ent_Loan_transfer()



